﻿
#ifndef _PLAYERINFO_MGR_H_
#define _PLAYERINFO_MGR_H_

#include <unordered_map>
#include <stdint.h>
#include <string>
#include <set>
#include <list>
#include "../share/common_singleton.h"
#include "../../3rd/libjson/value.h"
#include "LuaManager.h"


class CPlayerGuild
{
public:
	CPlayerGuild()
	{
		last_leave_guild_time_ = 0;
	}
	~CPlayerGuild() {}

	void LoadFromJson(Json::Value& playerguild_json);
	void SaveToJson(Json::Value& playerguild_json);

	int32_t leave_guild_time() const { return last_leave_guild_time_; }
	void set_leave_guild_time(int32_t leave_guild_time) { last_leave_guild_time_ = leave_guild_time; }
	int32_t GetJoinCDLimitTime();
private:
	int32_t last_leave_guild_time_;
};

class CPlayerInfo
{
public:
	CPlayerInfo(void)
	{
		m_accountId = 0;
		m_playerId = 0;
		m_name = "";
		m_union_name = "";
		m_union_id = 0;
		m_union_pos = 0;
		m_level = 0;
		m_heroid = 0;
		m_photoframe = 0;
		m_afc = 0;
		m_title = 0;
		m_heroSkinId = 0;
		m_vip = 0;
		last_logout_time_ = 0;
		m_last_save_time = 0;
	}

	~CPlayerInfo(void)
	{

	}

	void set_accountId(uint32_t account_id) { m_accountId = account_id; }
	uint32_t get_accountId() { return m_accountId; }

	void set_playerId(uint32_t playerid) { m_playerId = playerid; }
	uint32_t get_playerId() { return m_playerId; }

	void set_name(const std::string& name) { m_name = name; }
	std::string get_name() { return m_name; }

	void set_union_name(const std::string& union_name) { m_union_name = union_name; }
	std::string get_union_name() { return m_union_name; }

	void set_union_pos(int32_t pos) { m_union_pos = pos; }
	int32_t union_pos() { return m_union_pos; }

	void set_union_id(uint32_t union_id) { m_union_id = union_id; }
	uint32_t get_union_id() { return m_union_id; }

	void set_level(uint32_t level) { m_level = level; }
	uint32_t get_level() { return m_level; }

	void set_heroid(uint32_t heroid) { m_heroid = heroid; }
	uint32_t get_heroid() { return m_heroid; }

	void set_photoframe(uint32_t photoframe) { m_photoframe = photoframe; }
	uint32_t get_photoframe() { return m_photoframe; }

	void set_afc(uint32_t afc) { m_afc = afc; }
	uint32_t get_afc() { return m_afc; }

	void set_title(uint32_t title) { m_title = title; }
	uint32_t get_title() { return m_title; }

	void set_heroSkinId(uint32_t heroskinid) { m_heroSkinId = heroskinid; }
	uint32_t get_heroSkinId() { return m_heroSkinId; }

	void set_vip(uint32_t vip) { m_vip = vip; }
	uint32_t get_vip() { return m_vip; }

	void FillToJson(Json::Value& info);
	bool parseFromJson(const Json::Value& playerinfo);

	void set_last_save_time(int32_t time) { m_last_save_time = time; }
	int32_t last_save_time() { return m_last_save_time; }

	void set_last_logout_time(int32_t logout_time) { last_logout_time_ = logout_time; }
	int32_t last_logout_time() { return last_logout_time_; }

	CPlayerGuild& PlayerGuild() { return mPlayerGuild; }

	
private:
	uint32_t	m_accountId;
	uint32_t	m_playerId;
	std::string m_name;
	std::string m_union_name;	//工会名称
	uint32_t	m_union_id;		//工会ID
	int32_t		m_union_pos;	//公会职位
	uint32_t	m_level;		//等级
	uint32_t	m_heroid;		//当前英雄ID
	uint32_t	m_photoframe;	//头像框
	uint32_t	m_afc;			//战斗力
	uint32_t	m_title;		//称号ID
	uint32_t	m_heroSkinId;	//英雄皮肤ID
	uint32_t	m_vip;			//vip等级
	int32_t		last_logout_time_;//上次登出时间

	int32_t		m_last_save_time;

	CPlayerGuild mPlayerGuild;
};


//<playerid, CPlayerInfo>
typedef std::unordered_map< uint32_t, CPlayerInfo* > TRoleIDMap;
typedef std::unordered_map< uint32_t, uint32_t> TAccountIDMap;
typedef std::set< uint32_t > TRoleIDSet;
typedef std::unordered_map< std::string, CPlayerInfo* > TRoleNameMap;


//-------------------------------------------------------------------------------
class Player;
class LuaManager;
struct PlayerHero;

//-------------------------------------------------------------------------------
class CPlayerInfoMgr : public common::CSingleton< CPlayerInfoMgr >
{
private:
	TRoleIDMap role_id_map_;/**<角色id索引表  */
	TRoleIDSet role_id_set_;/**<在线角色id索引表*/
	TRoleNameMap role_name_map_;
	TAccountIDMap account_id_map_;// 账号角色ID索引表
	int32_t role_id_max_;//该服上角色ID最大值
private:
	bool LoadDB();

	void transformRecommend(std::string& recommandlist, std::set<uint32_t>& recommand);
public:
	CPlayerInfoMgr(void);
	~CPlayerInfoMgr();

public:
	bool Initialize(void);
	void Finalize(void);

	CPlayerInfo* GetPlayerInfoByPlayerID(uint32_t playerid);
	CPlayerInfo* GetPlayerInfoByPlayerName(const std::string& name);
	int32_t GetPlayerIDbyAccountID(uint32_t account_id);
	int32_t GetAccoutRoleIDMax();
	int32_t CheckRefreshRoleIDMax(int32_t role_id);

	int32_t GetPlayerCount() { return role_name_map_.size(); }

	void GetRecommendFriend(uint32_t playerid, const std::string& friendlist, std::string& recommandlist);

	void GetPlayerInfo(uint32_t playerid, std::string& playerinfo_str);
	void GetPlayerInfoByName(const std::string& name, std::string& playerinfo_str);

	void AddNewPlayer(std::string& playerinfo);
	void AddRole(CPlayerInfo* player_info);

	void PlayerChangeName(std::string& old_name, CPlayerInfo& playerinfo);
public:
	void PlayerChanged(int32_t player_id, int32_t change_time = 0);
	int32_t SaveRoles(int32_t max_save_count);

	static void OnEventPlayerLeaveGame(Player& player);

	Player* GetPlayerByClientKey(uint64_t client_key);

private:
	// 目前作为存储好友数据的作用
	struct SDirtyPlayer
	{
		int32_t player_id_;
		int32_t add_time_;
		SDirtyPlayer()
		{
			player_id_ = 0;
			add_time_ = 0;
		}
	};
	typedef std::list< SDirtyPlayer > TPlayerSaveList;
	TPlayerSaveList save_list_;
};

#endif // !_PLAYERINFO_MGR_H_


